package main.ServiceImpli;

/**
 * 作者: ALIM-MASTIK
 * 创建实现: 2023/7/15 9:23
 * 项目名称：chat
 * 软件: IntelliJ IDEA
 */
import main.Daoimpl.UserDAOImpl;
import main.Service.UserService;
import main.dao.UserDAO;
import main.pojo.User;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDAO userDAO=new UserDAOImpl();

    public UserServiceImpl(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
    public UserServiceImpl(){}

    @Override
    public User getUserByID(int userID)   {
        return userDAO.getUserByID(userID);
    }

    @Override
    public List<User> getAllUsers()   {
        return userDAO.getAllUsers();
    }

    @Override
    public Boolean createUser(User user)  {
        if (userDAO.createUser(user)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean updateUser(User user) {
        if (        userDAO.updateUser(user)!=null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean deleteUser(int userID)  {
        if (userDAO.deleteUser(userID)!=null){

            return true;
    }else {
            return false;
        }
    }
    @Override
    public Boolean CheckLogin(User user)   {
        User foundUser = userDAO.getUserByUsername(user.getUsername());
        try {
            if (foundUser != null && foundUser.getPassword().equals(user.getPassword())) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    @Override
    public User getUserByUsername(String username) {
      try {
          UserDAO userDAO=new UserDAOImpl();
          return userDAO.getUserByUsername(username);
      } catch (Exception e) {
          return null;
      }
    }

    @Override
    public User getUserByUserNameAndPassword(String username, String Password) {
        UserDAO userDAO=new UserDAOImpl();
        try {
            return userDAO.getUserByUserNameAndPassword(username,Password);
        } catch (Exception e) {
            return null;
        }
    }
}
